import { RESULT_CODES } from "@/api/helper/ResultCode";
import { getShopsAddressListApi } from "@/api/modules/merchant";
import { orderAfterSaleAgreeApi, orderAfterSaleRefuseApi, refundMoneyApi } from "@/api/modules/transactionManagement";
import { Modal, message } from "antd";
import { find } from "lodash";
import { DeliveryReturnValues } from "../merchant/Address/constant";
import useUrl from "@/hooks/useUrl";

interface Props {
	successCallback: () => void;
}

export default (props: Props) => {
	const { successCallback } = props;
	const { navigate } = useUrl();
	//通过
	const handleAgree = async (record: any) => {
		// const shop_id = record?.shop_id;
		// const res = await getShopsAddressListApi({ shop_id });
		// const default_refund_address = find(res?.data, i => i?.is_default === 1 && i?.category === DeliveryReturnValues.return);
		// if (!default_refund_address) {
		// 	Modal.confirm({
		// 		icon: null,
		// 		title: "提示",
		// 		content: "请先设置店铺的默认退货地址！",
		// 		closable: true,
		// 		onOk: c => {
		// 			navigate(`/merchants/address?shop_id=${shop_id}&name=${record?.shop_info?.shop_name}`);
		// 			c?.();
		// 		}
		// 	});
		// 	return;
		// }

		Modal.confirm({
			icon: null,
			title: "确认同意退款？",
			content: "确认后退款金额直接退回买家付款账户",
			closable: true,
			onOk: c => {
				orderAfterSaleAgreeApi({
					order_after_sale_id: record?.aftersales_info?.order_after_sale_id
				}).then(res => {
					if (res?.code === RESULT_CODES.SUCCESS) {
						refundMoneyApi({ order_after_sale_id: record?.aftersales_info?.order_after_sale_id }).then(res => {
							if (res?.code === RESULT_CODES.SUCCESS) {
								message.success("操作成功");
								successCallback?.();
								c?.();
							}
						});
					}
				});
			}
		});
	};

	//拒绝
	const handleRefuse = (record: any) => {
		Modal.confirm({
			icon: null,
			title: "确认拒绝退款？",
			content: "拒绝后买家可重新发起退款流程",
			closable: true,
			onOk: c => {
				orderAfterSaleRefuseApi({ order_after_sale_id: record?.aftersales_info?.order_after_sale_id }).then(res => {
					if (res?.code === RESULT_CODES.SUCCESS) {
						message.success("操作成功");
						successCallback?.();
						c?.();
					}
				});
			}
		});
	};

	return {
		onAgree: handleAgree,
		onRefuse: handleRefuse
	};
};
